<template>
	<view class="all-main">
		<u-loading-page :loading="loadding" loading-mode="semicircle" loading-text="Hello MAX"></u-loading-page>
		<view v-if="!loadding && dataList.length > 0" class="main">
			<view class="list">
				<u-swipe-action>
					<u-swipe-action-item :options="options1" v-for="(item, index) in dataList" :key="index"
						@click="delData(item)">
						<view class="items">
							<view class="left" @click="check_data(item)">
								<view class="area">
									{{item.province}}{{item.city}}{{item.district}}
								</view>
								<view class="address">
									{{item.address}}
								</view>
								<view class="contact">
									<text class="names">{{item.name}}</text>
									<text class="phone">{{item.mobile}}</text>
									<text v-if="item.default == 1" class="defaults">默认</text>
								</view>
							</view>
							<view class="right">
								<u-icon name="edit-pen-fill" @click="editData(item)"></u-icon>
							</view>
						</view>
					</u-swipe-action-item>
				</u-swipe-action>
			</view>
		</view>
		<u-empty v-else mode="address" icon="/static/Empty/data_null.png"></u-empty>
		<view class="bottom-btn">
			<text @click="addData">新增收货地址</text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				loadding: true,
				userinfo: {},
				dataList: [],
				options1: [{
					text: '删除'
				}],
				type: 1,
				order_id: null,
				sn: '',
				tp: 1,
				id: '',
				name: '',
			}
		},
		onLoad(e) {
			this.userinfo = uni.getStorageSync('userinfo');
			if (e.type != undefined) {
				this.type = e.type
			}
			if (e.order_id != undefined) {
				this.order_id = e.order_id
			}
			if (e.sn) {
				this.sn = e.sn
			}
			if (e.tp) {
				this.tp = e.tp
			}
			if (e.id) {
				this.id = e.id
			}
			if (e.name) {
				this.name = e.name
			}
			this.loadData()
		},
		onShow() {
			this.loadData()
		},
		methods: {
			// 添加收货地址
			addData() {
				uni.navigateTo({
					url: '/pages/version5/index/shop/address/add'
				})
			},
			// 修改收货地址
			editData(item) {
				uni.navigateTo({
					url: '/pages/version5/index/shop/address/edit?id=' + item.id
				})
			},
			// 选择收货地址
			check_data(item) {
				if (this.tp == 1) {
					if (item.default == 1) {
						uni.navigateTo({
							url: '/pages/version5/index/communication/exchange?type=' + this.type
						})
					} else {
						this.setAddressDefault(item.id)
					}
				} else if (this.tp == 2) {
					this.setApplyAddress(item.id)
				} else if (this.tp == 3) {
					if (item.default == 1) {
						uni.navigateTo({
							url: '/pages/version5/index/apply/detail?id=' + this.id + '&name=' + this.name
						})
					} else {
						this.setAddressDefault(item.id)
					}
				}
			},
			// 加载数据
			async loadData() {
				const {
					data: res
				} = await this.$myHttp5({
					url: 'Order/getAddress',
					header: {
						'token': this.userinfo.token
					}
				});
				this.loadding = false
				if (res.code == 1) {
					this.dataList = res.data
				}
			},
			// 删除收货地址 - 按钮
			delData(item) {
				var that = this
				uni.showModal({
					title: '提示',
					content: '确定要删除地址吗？',
					success: function(res) {
						if (res.confirm) {
							that.delDatas(item)
						}
					}
				});
			},
			// 删除收货地址 - 功能
			async delDatas(item) {
				var that = this
				uni.showLoading({
					title: '删除中'
				});
				const {
					data: res
				} = await this.$myHttp5({
					url: 'Order/delAddress',
					data: {
						id: item.id
					},
					header: {
						'token': this.userinfo.token
					}
				});
				uni.hideLoading()
				uni.showToast({
					title: res.msg,
					icon: "none"
				});
				if (res.code == 1) {
					setTimeout(function() {
						that.loadData()
					}, 1000)
				}
			},
			// 修改订单收货地址
			async editOrderAddress(item) {
				const {
					data: res
				} = await this.$myHttp5({
					url: 'Order/editOrderAddress',
					data: {
						order_id: this.order_id,
						address_id: item.id
					},
					header: {
						'token': this.userinfo.token
					}
				});
				if (res.code == 1) {
					uni.navigateBack({
						delta: 1, // 返回的页面数，如果为1表示返回上一页
					});
				}
			},
			// 设置默认收货地址
			async setAddressDefault(id) {
				uni.showLoading({
					title: '删除中'
				});
				const {
					data: res
				} = await this.$myHttp5({
					url: 'Order/setAddressDefault',
					data: {
						address_id: item.id
					},
					header: {
						'token': this.userinfo.token
					}
				});
				uni.hideLoading()
				if (res.code == 1) {
					if (this.tp == 1) {
						uni.navigateTo({
							url: '/pages/version5/index/communication/exchange?type=' + this.type
						})
					} else if (this.tp == 3) {
						uni.navigateTo({
							url: '/pages/version5/index/apply/detail?id=' + this.id + '&name=' + this.name
						})
					}
				}
			},
			// 设置申领地址
			async setApplyAddress(id) {
				uni.showLoading({
					title: '加载中'
				});
				const {
					data: res
				} = await this.$myHttp5({
					url: 'v1/editApply',
					data: {
						id: this.order_id,
						address_id: id
					},
					header: {
						'token': this.userinfo.token
					}
				});
				uni.hideLoading()
				uni.navigateTo({
					url: '/pages/version5/mine/apply/apply'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #f3f4f6;
	}
	.all-main {

		.main {
			padding: 20rpx;

			.list {
				background-color: #ffffff;
				border-radius: 10rpx;

				.items {
					display: flex;
					justify-content: space-between;
					padding: 20rpx;
					border-bottom: 1px solid #f4f4f5;

					.left {
						width: 93%;

						.area {
							font-size: 24rpx;
							color: #c8c9cc;
							padding-bottom: 10rpx;
						}

						.address {
							font-size: 30rpx;
							font-weight: bold;
							padding-bottom: 10rpx;
						}

						.contact {
							font-size: 28rpx;

							.phone {
								color: #c8c9cc;
								padding: 0 16rpx;
							}

							.defaults {
								font-size: 24rpx;
								background-color: #fa3534;
								border-radius: 10rpx;
								color: #ffffff;
								padding: 4rpx 10rpx;
							}
						}
					}

					.right {
						width: 5%;
						display: flex;
						justify-content: center;
					}
				}
			}
		}

		.bottom-btn {
			position: fixed;
			bottom: 0;
			left: 0;
			right: 0;
			height: 100rpx;
			background-color: #ffffff;
			text-align: center;

			text {
				margin: 0 auto;
				margin-top: 10rpx;
				display: block;
				width: 80%;
				padding: 20rpx;
				background-color: #fa3534;
				color: #ffffff;
				font-size: 30rpx;
				border-radius: 50rpx;
			}
		}
	}
</style>